/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aem.sticky.dialog;

import com.aem.sticky.StickyListener;
import com.aem.sticky.button.TextButton;
import com.aem.sticky.button.events.SimpleClickListener;
import org.newdawn.slick.*;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.geom.Shape;


/**
 *
 * @author abacs
 */
public class Notification {
	
	/** Center x coordinate. */
	private int centerX = 0;
	/** Center y coordinate. */
	private int centerY = 0;
	
	private TextButton textButton = null;
	
	private SimpleClickListener clickListener = null;
	
	private String bodyText = "";
	
	private String buttonText = "";
	
	private boolean show = false;
	
	protected Shape shape = new Rectangle(-1,-1,0,0);
	
	/** Text padding in pixels */
	protected int padding = 6;
	
	private Color color = Color.white;
	
	private Color backgroundColor = Color.black;
	
	// ---------------------------------------------------------------------
	// constructors:
	
	
	public Notification(int x, int y, String bodyText, String buttonText) {
		this.centerX = x;
		this.centerY = y;
		this.bodyText = bodyText;
		this.buttonText = buttonText;
	}
	
	
	// ---------------------------------------------------------------------
		
		
	/**
	 * 
	 * @param container
	 * @param listener
	 * @param buttonSound
	 * @throws SlickException
	 */
	public void init(
			GameContainer container,
			StickyListener listener,
			Sound buttonSound
	) throws SlickException {
		this.createTextButton(container, listener, buttonSound);
	}
	
	/**
	 * 
	 * @param container
	 * @param delta
	 * @throws SlickException
	 */
	public void update(GameContainer container, int delta)
		throws SlickException {
		
		if(this.textButton != null)
			this.textButton.update(container, delta);
	}
	
	/**
	 *  Allows to draw the world.
	 *  */
	public void render(
			GameContainer container,
			Graphics graphics
	) throws SlickException {
		
		if(this.show == false)
			return;
		
		if(this.shape.getWidth() == 0){
			Font font = graphics.getFont();
			
			int width = font.getWidth(this.bodyText) + this.padding * 2;
			int height = font.getHeight(this.bodyText) + this.padding * 2 * 2;
			this.shape = new Rectangle(
					this.centerX - width/2,
					this.centerY - height,
					width,
					height
			);
			
			int buttonWidth = font.getWidth(this.buttonText) + 4;
			
			// also set the button position:
			this.textButton.moveTo(this.centerX - buttonWidth / 2, this.shape.getCenterY());
		}
		
		graphics.setColor(this.backgroundColor);
		graphics.fill(this.shape);
		graphics.setColor(this.color);
		graphics.draw(this.shape);
		
		graphics.setColor(this.color);
		
		// draw it:
		graphics.drawString(
				this.bodyText,
				this.shape.getX() + this.padding,
				this.shape.getY() + this.padding
		);
		
		if(this.textButton != null)
			this.textButton.render(container, graphics);
		
	}
	
	// ---------------------------------------------------------------------
	
	/**
	 * 
	 * @param container
	 * @param listener
	 * @param buttonSound
	 * @throws SlickException
	 */
	private void createTextButton(
			GameContainer container,
			final StickyListener listener,
			Sound buttonSound
	) throws SlickException {
		
		this.textButton = 
			new TextButton(centerX, centerY, this.buttonText, buttonSound);
			
		listener.add(this.textButton);
			
		this.textButton.addListener(new SimpleClickListener() {
			@Override
			public void onClick() {
				hide();
				clickListener.onClick();
			}
		});
	}
	
	// -------------------------------------------------------------------------
	
	/**
	 * Hides the dialog.
	 */
	public void hide() {
		this.show = false;
		this.shape = new Rectangle(0,0,0,0);
		this.textButton.hide();
	}
	
	/**
	 * Displays the dialog.
	 */
	public void show() {
		this.show = true;
		this.shape.setX(this.centerX);
		this.shape.setY(this.centerY);
	}
	
	/**
	 * Displays the dialog.
	 */
	public boolean isVisible() {
		return this.show;
	}
	
	/**
	 * 
	 */
	public void setButtonText(String buttonText) {
		this.buttonText = buttonText;
		if(this.textButton != null)
			this.textButton.setText(buttonText);
	}
	
	/**
	 * 
	 */
	public void setBodyText(String bodyText) {
		this.bodyText = bodyText;
	}
	
	public void setColor(Color color) {
		this.color = color;
	}
	
	public void setBackgroundColor(Color color) {
		this.backgroundColor = color;
	}
	
	public void setPadding(int padding) {
		this.padding = padding;
	}
	
	/**
	 * 
	 * @param centerX center position x
	 * @param centerY center position y
	 */
	public void moveTo(int centerX, int centerY) {
		this.centerX = centerX;
		this.centerY = centerY;
		this.shape = new Rectangle(0, 0, 0, 0);
	}
	
	/**
	 * 
	 * @param listener
	 */
	public void addListener(SimpleClickListener listener) {
		this.clickListener = listener;
	}
	
}
